package com.luoxiang.socialandroid.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.Typeface;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.HomepageAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.fragment.FansFragment;
import com.luoxiang.socialandroid.fragment.FollowFragment;
import com.luoxiang.socialandroid.fragment.FriendFollowFragment;

import java.util.ArrayList;

public class FfActivity extends BaseActivity {
    private TabLayout tab_ff;
    private ImageView icon_back;
    private ViewPager vp_ff;
    private ArrayList<String> list = new ArrayList<>();
    private ArrayList<Fragment> fragmentlist = new ArrayList<>();
    private int focusLogNum = 0;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_ff);
        setStatusBarTranslucent();
        focusLogNum = getIntent().getIntExtra("focusLogNum", 0);
    }

    @Override
    protected void initView() {

        tab_ff = findViewById(R.id.tab_ff);
        icon_back = findViewById(R.id.icon_back);
        vp_ff = findViewById(R.id.vp_ff);
        list.clear();
        fragmentlist.clear();
        list.add(getString(R.string.mi_guanzhu));
        list.add(getString(R.string.mi_fensi));
        fragmentlist.add(new FriendFollowFragment());
        fragmentlist.add(new FansFragment());
        HomepageAdapter pagerAdapter = new HomepageAdapter(getSupportFragmentManager(), fragmentlist, list);
        vp_ff.setAdapter(pagerAdapter);
        tab_ff.setupWithViewPager(vp_ff);
        setTabStyle();

        tab_ff.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTextColor(Color.parseColor("#333333"));
                tv.setTextSize(16);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                customView.findViewById(R.id.imagePoint).setVisibility(View.GONE);
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                TextView tv = customView.findViewById(R.id.tab_tv);
                tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                tv.setTextSize(16);
                tv.setTextColor(Color.parseColor("#666666"));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

    }

    @SuppressLint({"WrongConstant", "ResourceType"})
    private void setTabStyle() {
        //根据Tab数量循环来设置
        for (int i = 0; i < list.size(); i++) {
            TabLayout.Tab tab = this.tab_ff.getTabAt(i);
            if (tab != null) {
                View view = LayoutInflater.from(FfActivity.this).inflate(R.layout.text_tablayout, null);
                //设置Tab标题
                ((TextView) view.findViewById(R.id.tab_tv)).setText(list.get(i));
                //第一个默认为选择样式
                if (i == 0) {
                    ((TextView) view.findViewById(R.id.tab_tv)).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                } else {
                    if (focusLogNum > 0) {
                        view.findViewById(R.id.imagePoint).setVisibility(View.VISIBLE);
                    } else {
                        view.findViewById(R.id.imagePoint).setVisibility(View.GONE);
                    }
                }
                //最后添加view到Tab上面
                tab.setCustomView(view);
            }
        }
    }
}
